<html>
<style type="text/css">

div.cppCode
{
	background-color: #eeeeee;
	border: 1px solid black;
	padding: 10px;
	font-family: courier;
	white-space: pre;
}
span.comment
{
	color: #888888; 
	font-style: italic; 
}

span.string
{
	color: #0000ff; 
}

span.specialChar
{
	font-weight: bold; 
}

span.preproc
{
	color: #00aa00;
}

span.keyword
{
	color: #0000ff; 
	font-weight: bold; 
}

</style>

<div id="src1" class="cppCode">	
#include &lt;windows.h>
#include &lt;winsock.h>
#include &lt;stdio.h>

SOCKET sock;

void ErrorCheck(char* s);

sockaddr_in bcastAddr;

bool initNetwork()
{
	// Socket and error variables
	int err;

	// Initialize Winsock
	WSADATA wsadata;
	err = WSAStartup( MAKEWORD( 2, 2 ), &wsadata );

	if( err ) ErrorCheck("Initializing Winsock");

	// Create socket
	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	if( sock == INVALID_SOCKET ) ErrorCheck("Creating socket");

	// Bind socket
	SOCKADDR_IN saBind;
	saBind.sin_family = AF_INET;
	saBind.sin_port = htons( 6111 );
	saBind.sin_addr.s_addr = INADDR_ANY;

	err = bind(sock, (const sockaddr*)&saBind, sizeof(saBind));

	if( err ) ErrorCheck("Binding socket");

/*	// Set socket to nonblocking
	unsigned long argp = 1;
	err = ioctlsocket( sock, FIONBIO, &argp );

	if( err ) ErrorCheck("Setting socket to nonblocking");*/

	// Enable broadcasts
	int On = 1;
	err = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char *)&On, sizeof(On));

	if( err ) ErrorCheck("Enabling broadcasts");

	bcastAddr.sin_family = AF_INET;
	bcastAddr.sin_port = htons( 6111 );
	bcastAddr.sin_addr.s_addr = INADDR_BROADCAST;

	printf("All ok\n");
	return true;
}

bool shutdownNetwork()
{
	return true;
}

void ErrorCheck(char *s)
{
	printf("\n\nERROR: %s\n",s);
	shutdownNetwork();
	exit(0);
}
void main()
{
	if (initNetwork())
	{
		unsigned char data[] = {0xad, 0x2b, 0x14, 0x00, 0x02, 0x00, 0x00, 0x00, 0x50, 0x58, 0x45, 0x53, 0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
		
		sendto(sock, (char*)data, data[2], 0, (struct sockaddr *)(&bcastAddr), sizeof(bcastAddr));

		printf("Sent\n");
		unsigned char buf[1024];

		int n;
		int l;
		struct sockaddr_in sock_addr;

		while (1)
		{
		n = sizeof(struct sockaddr_in);
		if ((l = recvfrom(sock, (char*)buf, 1024, 0, (struct sockaddr *)&sock_addr, &n)) &lt; 0) 
		{
			ErrorCheck("Could not read from UDP socket\n");
		} 


		if (n>0)
		{
			printf("Received from %ld \n",sock_addr.sin_addr);
		for (int i=0;i&lt;n;i++)
			printf("0x%02x ",buf[i]);
		printf("\n");
		}
		}
	}

	shutdownNetwork();
/*
	if (sock != INVALID_SOCKET)
		closesocket(sock);
//	WSACleanup();
*/
}
</div>


<script language="JavaScript">
	
	var hiText;
	var outputText;
	
	keywords = "bool break case catch char class const continue default delete do double else enum explicit extern false float for friend goto if inline int long mutable namespace new operator private protected public register return short signed sizeof static struct switch template this throw true try typedef typeid typename union unsigned virtual void volatile while".split(" ");
	
	function matchCComment()
	{
		if (hiText.substr(0,2)=='/*')
		{
			p = 2;
			while (hiText.substr(p,2)!='*/')
				p++;
			p+=2;
			outputText += "<span class='comment'>"+hiText.substr(0,p)+"</span>";
			hiText = hiText.substr(p);
			return true;
		}
		return false;
	}

	function matchCppComment()
	{
		if (hiText.substr(0,2)=='//')
		{
			p = 2;
			while (hiText.charAt(p)!='\n')
				p++;
			
			outputText += "<span class='comment'>"+hiText.substr(0,p)+"</span>";
			hiText = hiText.substr(p);
			return true;
		}
		return false;
	}

	function highlightSpecialChars(str)
	{
		res="";
		i=0;
		while (i<str.length)
		{
			if (str.charAt(i)=='\\')
			{
				res += "<span class='specialChar'>"+str.substr(i,2)+"</span>";				
				i+=2;
			}
			else
			{
				res += str.charAt(i);
				i++;
			}			
		}
		return res;
	}

	function matchString(chr)
	{
		if (hiText.charAt(0)==chr)
		{
			p = 1;
			while ((hiText.charAt(p)!=chr)||((hiText.charAt(p)==chr)&&(hiText.charAt(p-1)=='\\')))
				p++;
			p++;
			
			outputText += "<span class='string'>"+highlightSpecialChars(hiText.substr(0,p))+"</span>";
			hiText = hiText.substr(p);
			return true;
		}
		return false;
	}

	function matchPreprocessor()
	{
		if (hiText.charAt(0)=='#')
		{
			p = 1;
			while ((hiText.charAt(p)!='\n')||((hiText.charAt(p)=='\n')&&(hiText.charAt(p-1)=='\\')))
				p++;
			
			outputText += "<span class='preproc'>"+hiText.substr(0,p)+"</span>";
			hiText = hiText.substr(p);
			return true;
		}
		return false;
	}
	
	function isTokenChar(c)
	{
		if ((c=='_')||((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z'))||((c>='0')&&(c<='9')))
			return true;
			
		return false;
	}
	function matchKeyword()
	{
		if (isTokenChar(hiText.charAt(0)))
		{
			p=1;
			while (isTokenChar(hiText.charAt(p)))
				p++;
			
			t = hiText.substr(0,p);
			
			out = false;
			for (i=0;i<keywords.length;i++)
				if (t == keywords[i])
					out=true;
			
			if (out)	
				outputText += "<span class='keyword'>"+t+"</span>";
			else
				outputText += t;
			hiText = hiText.substr(p);
			return true;
		}
		return false;
	}

	function matchOther()
	{
		outputText += hiText.charAt(0);
		hiText = hiText.substr(1);
	}


	function highlightCppSyntax(divID)
	{
		var theDiv = document.getElementById(divID);
		
		hiText = theDiv.innerHTML;
		hiText = hiText.replace("\r\n","\n");
		hiText = hiText.replace("\r","\n");
		
		outputText="";

		while (hiText.length>0)
		{
			if (matchCComment())
			{
			}
			else
			if (matchCppComment())
			{
			}
			else
			if (matchString('"'))
			{
			}
			else
			if (matchString("'"))
			{
			}
			else
			if (matchPreprocessor())
			{
			}
			else
			if (matchKeyword())
			{
			}
			else
				matchOther();
				
//			alert(hiText);
		}
		
		
/*		lines = outputText.split("\n");
		s = "";
		for (i=0;i<lines.length;i++)
			s += i+"\t"+lines[i]+"\n";
*/			
		theDiv.innerHTML = outputText;
	}
	
	highlightCppSyntax("src1");
	
</script>
		
</html>